//
//  UIButton+Extensions.swift
//  SwiftDemo
//
//  Created by Chris on 2017/6/13.
//  Copyright © 2017年 chris. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// 创建文本按钮
    ///
    /// - Parameters:
    ///   - title: 标题文字
    ///   - fontSize: 字体大小
    ///   - normalColor: 默认颜色
    ///   - highlightedColor: 高亮颜色
    /// - Returns: UIButton
    class func cj_init(_ title: String, fontSize: CGFloat, normalColor: UIColor, highlightedColor: UIColor) -> UIButton {
        return self.cj_init(title, fontSize: fontSize, normalColor: normalColor, highlightedColor: highlightedColor, backgroundImageName: nil)
    }
    
    /// 创建文本按钮
    ///
    /// - Parameters:
    ///   - title: 标题文字
    ///   - fontSize: 字体大小
    ///   - normalColor: 默认颜色
    ///   - highlightedColor: 高亮颜色
    ///   - backgroundImageName: 背景图片名
    /// - Returns: UIButton
    class func cj_init(_ title: String, fontSize: CGFloat, normalColor: UIColor, highlightedColor: UIColor, backgroundImageName: String?) -> UIButton {
        
        let button = UIButton()
        
        button.setTitle(title, for: .normal)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        button.setTitleColor(normalColor, for: .normal)
        button.setTitleColor(highlightedColor, for: .highlighted)
        
        if backgroundImageName != nil {
            button.setBackgroundImage(UIImage(named:backgroundImageName!), for: .normal)
            button.setBackgroundImage(UIImage(named:backgroundImageName! + "_highlighted"), for: .highlighted)
        }
        button.sizeToFit()
        
        return button
    }
    
    /// 创建图片按钮
    ///
    /// - Parameters:
    ///   - imageName: 图片名
    ///   - backgroundImageName: 背景图片名
    /// - Returns: UIButton
    class func cj_init(_ imageName: String, backgroundImageName: String) -> UIButton {
        
        let button = UIButton()
        
        button.setImage(UIImage(named:imageName), for: .normal)
        button.setImage(UIImage(named:imageName + "_highlighted"), for: .highlighted)
        
        button.setBackgroundImage(UIImage(named:backgroundImageName), for: .normal)
        button.setBackgroundImage(UIImage(named:backgroundImageName + "_highlighted"), for: .highlighted)
        
        button.sizeToFit()
        
        return button
        
    }
}
